import device from './device';
let version = null;
let jsBridgeRegisterHandle = {};
jsBridgeRegisterHandle.install = function(Vue) {
  // console.warn('优先挂载桥');
  let jsBridge = Vue.prototype.$jsBridge;
  let jsBridgeCmd = Vue.prototype.$jsBridgeCmd;

  Vue.prototype.$myPlugs = {
    connectWIFI(data, callback) {
      if (device.webView) {
        //过滤掉pc端，wx小程序等等
        console.info('连接指定wifi');
        jsBridge.callHandler(jsBridgeCmd['connectWIFI'], data, function(
          response
        ) {
          callback && callback(response);
        });
      }
    },
    downloadZip(url, md5) {
      if (device.webView) {
        console.info('调用下载远程更新包', md5);
        jsBridge.callHandler(
          jsBridgeCmd['downloadZip'],
          {
            url: url,
            md5: md5
          },
          function(res) {
            console.log(res, '-------下载更新包之后的回调--------');
          }
        );
      } else {
        console.log('不是webview不需要下载');
      }
    },
    // 获取当前客户端信息 包含微信在内
    getAppInfo(callback) {
      new Promise(resolve => {
        if (device.webView) {
          jsBridge.callHandler(jsBridgeCmd['getAppInfo'], {}, res => {
            resolve(res);
          });
        } else {
          version = window.wxJsSdkVersion || '0.0.0';
          resolve({
            version
          });
        }
      }).then(res => {
        console.log('平台信息', res);
        localStorage.AppInfo = JSON.stringify(res);
        callback && callback();
      });
    },
    // 是否开启沉浸式
    statusBarHeight(callback) {
      if (device.webView) {
        jsBridge.callHandler(jsBridgeCmd['statusBarHeight'], {}, function(res) {
          console.log('获取顶部栏高度', res);
          callback & callback(res);
        });
      }
    }
  };
};
export default jsBridgeRegisterHandle;
